﻿using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LightCAD.Core
{
    public class ElementStyleConverter : JsonConverter<ElementStyle>
    {
        public override ElementStyle? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var str = reader.GetString();
            return ElementStyle.Parse(str);
        }

        public override void Write(Utf8JsonWriter writer, ElementStyle value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString());
        }
    }

    public class Vector2dConverter : JsonConverter<Vector2d>
    {
        public override Vector2d Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var str = reader.GetString();
            var parts = str.Split(',');
            return new Vector2d(double.Parse(parts[0]), double.Parse(parts[1]));
        }

        public override void Write(Utf8JsonWriter writer, Vector2d value, JsonSerializerOptions options)
        {
            writer.WriteStringValue($"{value.X.ToString("0.00")},{value.Y.ToString("0.00")}");
        }
    }
    public class DoubleConverter : JsonConverter<double>
    {
        public override double Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            var str = reader.GetString();
            return double.Parse(str);
        }

        public override void Write(Utf8JsonWriter writer, double value, JsonSerializerOptions options)
        {
            writer.WriteStringValue(value.ToString("0.00"));
        }
    }
}
